import datetime as dt

from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.oxml import OxmlElement, ns
from docx.oxml.ns import qn
from docx.shared import Pt, Cm
from docx.shared import RGBColor


def set_style(run):
    run.bold = False
    run.font.color.rgb = RGBColor(0, 0, 0)
    run.font.size = Pt(10)
    run.font.name = "仿宋_GB2312"
    run._element.rPr.rFonts.set(qn("w:eastAsia"), "仿宋_GB2312")


def add_header(document, project, report_type_cn):
    header = document.sections[0].header
    document.sections[0].different_first_page_header_footer = True
    header_text = project.project_info.name + report_type_cn
    header_paragraph = header.add_paragraph("")
    header_paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.RIGHT
    run = header_paragraph.add_run(header_text)
    set_style(run)


def create_element(name):
    return OxmlElement(name)


def create_attribute(element, name, value):
    element.set(ns.qn(name), value)


def add_page_number(paragraph):
    paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
    page_num_run = paragraph.add_run()
    set_style(page_num_run)
    fldChar1 = create_element("w:fldChar")
    create_attribute(fldChar1, "w:fldCharType", "begin")

    instrText = create_element("w:instrText")
    create_attribute(instrText, "xml:space", "preserve")
    instrText.text = "PAGE"

    fldChar2 = create_element("w:fldChar")
    create_attribute(fldChar2, "w:fldCharType", "end")

    page_num_run._r.append(fldChar1)
    page_num_run._r.append(instrText)
    page_num_run._r.append(fldChar2)

    slash_run = paragraph.add_run()
    t2 = create_element("w:t")
    create_attribute(t2, "xml:space", "preserve")
    t2.text = "/"
    slash_run._r.append(t2)
    set_style(slash_run)
    fldChar3 = create_element("w:fldChar")
    create_attribute(fldChar3, "w:fldCharType", "begin")

    instrText2 = create_element("w:instrText")
    create_attribute(instrText2, "xml:space", "preserve")
    instrText2.text = "NUMPAGES"

    fldChar4 = create_element("w:fldChar")
    create_attribute(fldChar4, "w:fldCharType", "end")

    num_pages_run = paragraph.add_run()
    num_pages_run._r.append(fldChar3)
    num_pages_run._r.append(instrText2)
    num_pages_run._r.append(fldChar4)
    set_style(num_pages_run)


def generate_front_page(document, project, report_type, report_type_cn):
    add_header(document, project, report_type_cn)
    add_page_number(document.sections[0].footer.paragraphs[0])
    logo = document.add_heading()
    # logo.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
    run = logo.add_run("")
    run.add_picture(f"image/{report_type}/logo.png", width=Cm(8))

    run.add_break()
    run.add_break()

    title_1 = document.add_heading("", level=1)
    title_1.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
    run = title_1.add_run(project.project_info.name)
    run.bold = True
    run.font.color.rgb = RGBColor(0, 0, 0)
    run.font.size = Pt(22)
    run.font.name = "楷体_GB2312"
    run._element.rPr.rFonts.set(qn("w:eastAsia"), "楷体_GB2312")

    title_2 = document.add_heading("", level=1)
    title_2.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
    run = title_2.add_run(report_type_cn)
    run.bold = True
    run.font.color.rgb = RGBColor(0, 0, 0)
    run.font.size = Pt(22)
    run.font.name = "楷体_GB2312"
    run._element.rPr.rFonts.set(qn("w:eastAsia"), "楷体_GB2312")

    run.add_break()

    image = document.add_heading()
    image.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
    run = image.add_run("")
    run.add_picture(f"image/{report_type}/frontpage.jpg", height=Cm(8))

    run.add_break()
    run.add_break()
    run.add_break()

    author = document.add_paragraph("")
    author.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
    run = author.add_run("国家电投集团风电产业创新中心")
    run.bold = True
    run.font.size = Pt(14)
    run.font.name = "仿宋_GB2312"
    run._element.rPr.rFonts.set(qn("w:eastAsia"), "仿宋_GB2312")
    date_str = dt.datetime.now().strftime("%Y年%m月%d日")
    date = document.add_paragraph()
    date.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
    run = date.add_run(date_str)
    run.bold = True
    run.font.size = Pt(14)
    run.font.name = "仿宋_GB2312"
    run._element.rPr.rFonts.set(qn("w:eastAsia"), "仿宋_GB2312")
    return document
